package com.huizhonglm.diap.presenters.wallet;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.user.MyCashListParams;
import com.huizhonglm.diap.api.model.user.MyCollectionListParams;
import com.huizhonglm.diap.api.model.wallet.CashInfo;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.wallet.iView.MyCashIView;

import java.util.List;

/**
 * 描述当前类的作用
 * 处理我的现金明细
 */
public class MyCashPresenter extends BasePresenter {

    private MyCashIView iView;

    private int pageIndex;

    public MyCashPresenter(MyCashIView iView) {
        this.iView = iView;
    }

    public void loadData(final String pullFlag, String userId){
        MyCashListParams params = new MyCashListParams();
        params.userId = userId;
        switch (pullFlag) {
            case Constants.LOAD_INIT:
            case Constants.LOAD_DROP:
                pageIndex = 1;
                break;
            case Constants.LOAD_MORE:
                pageIndex ++;
                break;
        }
        params.pageNumber = pageIndex;
        API.http().post("cashList", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    List<CashInfo> list = JSON.parseArray(result, CashInfo.class);
                    if (!EmptyUtil.isEmpty(list)) {
                        iView.onLoadDataSuccess(pullFlag, list);
                    } else {
                        iView.onLoadDataEmpty(pullFlag);
                    }
                } else {
                    iView.onLoadDataEmpty(pullFlag);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onLoadDataFailure(pullFlag);
            }
        });
    }

}
